//
//  WBtabBar.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/12.
//  Copyright © 2016年 木木. All rights reserved.
//

import UIKit

class WBtabBar: UITabBar {

    var clickAddBtn: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(addBtn)
    }
    
    //不允许加载XIB
    required init?(coder aDecoder: NSCoder) {
//        否则会报错,将下面错误注释
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        addSubview(addBtn)
    }
    
    
    lazy var addBtn: UIButton = {
        let btn = UIButton()
        btn .setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(cliclBtn), for: UIControlEvents.touchUpInside)
        
        return btn
    }()
    
  @objc private func cliclBtn(){
        clickAddBtn?()
    }
    
    //MARK: 布局
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let W = UIScreen.main.bounds.width / 5
        let H = self.bounds.height
    
        var index = 0
        for subView in subviews {
      
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRect(x:CGFloat(index) * W, y: 0, width: W, height: H)
                
                if index == 1 {
                    index =  index + 1
                    addBtn.frame = CGRect(x:CGFloat(index) * W, y: 0, width: W, height: H)
                }
                
                index += 1
                
            }
            
        }
    }
    
    
    

}







